package com.athui.utils.explorer.metadata.impl;

import com.athui.bean.metadata.ColumnMetaData;
import com.athui.bean.config.DataType;
import com.athui.bean.metadata.TableMetaData;
import com.athui.utils.common.ConfigureUtils;
import com.athui.utils.common.jdbc.JdbcResultSetBuilder;
import com.athui.utils.explorer.metadata.JdbcExplorer;
import org.apache.flink.api.common.typeinfo.BasicTypeInfo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: TODO
 * @autor: zhangzhonghui
 * @create: 2024-08-05 18:13
 * @Version: 1.0
 */
public class SqlServerJdbcExplorer extends JdbcExplorer {

    private Map<String,String> columnCommentMap = null;

    @Override
    protected void open(String schemaName,String tableName) {
        columnCommentMap = operation(new JdbcResultSetBuilder<Map<String, String>>() {
            @Override
            public String buildSql() {
                Map<String, Object> params = new HashMap<>();
                params.put("schema_name", schemaName);
                params.put("table_name", tableName);
                return ConfigureUtils.mapperParser("sqlServerColumnCommentQuery", params).toString();
            }

            @Override
            public Map<String,String> accept(ResultSet rs) throws SQLException {
                Map<String,String> map= new HashMap<>();
                while (rs.next()) {
                    String columnName = rs.getString("column_name");
                    String columnComment = rs.getString("column_comment");
                    map.put(columnName, columnComment);
                }
                return map;
            }
        });
    }

    @Override
    public Map<String, DataType> typeMapping() {
        Map<String, DataType> typeMapping = new HashMap<>();
        // 字符型
        typeMapping.put("varchar", new DataType(BasicTypeInfo.STRING_TYPE_INFO,DataType.TEXT));

        // 数值型
        typeMapping.put("int", new DataType(BasicTypeInfo.INT_TYPE_INFO, DataType.NUMERICAL));
        typeMapping.put("numeric", new DataType(BasicTypeInfo.BIG_DEC_TYPE_INFO, DataType.NUMERICAL));
        typeMapping.put("bigint", new DataType(BasicTypeInfo.LONG_TYPE_INFO, DataType.NUMERICAL));

        // 日期型
        typeMapping.put("datetime", new DataType(BasicTypeInfo.DATE_TYPE_INFO,DataType.DATE));
        typeMapping.put("datetime2", new DataType(BasicTypeInfo.DATE_TYPE_INFO,DataType.DATE));

        return typeMapping;
    }

    @Override
    public ColumnMetaData columnMetaDataBuilder(ColumnMetaData columnMetaData) {
        // 设置描述
        columnMetaData.setColumnComment(columnCommentMap.get(columnMetaData.getColumnName()));
        return columnMetaData;
    }

    @Override
    public TableMetaData tableMetaDataBuilder(String schemaName,String tableName) {
        return operation(new JdbcResultSetBuilder<TableMetaData>() {
            @Override
            public String buildSql() {
                Map<String, Object> params =new HashMap<>();
                params.put("schema_name",schemaName);
                params.put("table_name",tableName);
                return ConfigureUtils.mapperParser("sqlServerTableMetadataSql",params).toString();
            }

            @Override
            public TableMetaData accept(ResultSet rs) throws SQLException {
                TableMetaData tableMetaData = new TableMetaData();
                while (rs.next()){
                    tableMetaData.setSchemaName(rs.getString("schema_name"));
                    tableMetaData.setTableName(rs.getString("table_name"));
                    tableMetaData.setTableType(rs.getString("table_type"));
                    tableMetaData.setTableComment(rs.getString("table_comment"));
                    tableMetaData.setCreateTime(rs.getString("create_time"));
                    tableMetaData.setLastUpdateTime(rs.getString("last_update_time"));
                    tableMetaData.setRowSize(rs.getInt("row_size"));
                    tableMetaData.setDataSizeKB(rs.getLong("data_size_kb"));
                }
                return tableMetaData;
            }
        });
    }
}
